﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using XDeal.BL.Implements;
using Utility;
using Utility.Enums;
using XDeal.Common.Web;
using XDeal.Common.Constants;
using XDeal.DAL.Entities;
using XDeal.DAL.CustomEntities;


namespace XDEALWEB.Controllers
{
    public class GioHangController : Controller
    {
        //
        // GET: /GioHang/
        

        public ActionResult Index()
        {
            ViewData[ConstViewDataName.ListOfOrder] = GlobalVariables.OrderDeals;
            return View();
        }
        public RedirectResult AddToCart(int dealid)
        {
            Deal deal = new Deal();
            DealBL dealBL = new DealBL();
            deal = dealBL.GetValidDealByID(dealid);


            if (deal != null)
            {
                CusOrderBO cusOrder;
                if (GlobalVariables.OrderDeals == null)
                {
                    cusOrder = new CusOrderBO();
                }
                else
                {
                    cusOrder = GlobalVariables.OrderDeals;
                }

                bool ischeckDeal = false;
                foreach (OrderDetail item in cusOrder.OrderDetails)
                {
                    if (dealid ==ConvertManage.ConvertToInt(item.DealID_i,0))
                    {
                        item.Quantity_i++;
                        ischeckDeal = true;
                    }
                }
                if (!ischeckDeal)
                {
                    OrderDetail odt = new OrderDetail();
                    odt.DealID_i = dealid;
                    odt.Quantity_i = 1;
                    cusOrder.OrderDetails.Add(odt);
                }

                
                GlobalVariables.OrderDeals = cusOrder;
            }

            return Redirect(Url.Content("~/GioHang"));
        }

        [HttpPost]
        public ActionResult DeleteItemInCart(int deleteDealID)
        {
            if (DeleteItemFromCart(deleteDealID))
            {
                return Content("1");
            }
            return Content("0");
        }

        [HttpPost]
        public ActionResult UpdateCartItem(int dealidupdate, int quantity)
        {
            UpdateCartQuantity(dealidupdate, quantity);
            return Content("1");
        }
        /// <summary>
        /// Cap nhap so luong mua vao Gio hang
        /// </summary>
        /// <param name="dealidupdate"></param>
        /// <param name="quantity"></param>
        private void UpdateCartQuantity(int dealidupdate, int quantity)
        {
            CusOrderBO customOrder = new CusOrderBO();
            if (GlobalVariables.OrderDeals != null)
            {
                customOrder = GlobalVariables.OrderDeals;
                foreach (OrderDetail odt in customOrder.OrderDetails)
                {
                    if (odt.DealID_i == dealidupdate)
                    {
                        odt.Quantity_i = quantity;
                    }
                }
                GlobalVariables.OrderDeals = customOrder;
            }

        }

        private bool DeleteItemFromCart(int dealID)
        {
            CusOrderBO customOrder = new CusOrderBO();
            if (GlobalVariables.OrderDeals != null)
            {
                customOrder = GlobalVariables.OrderDeals;
                foreach (OrderDetail odt in customOrder.OrderDetails)
                {
                    if (odt.DealID_i == dealID)
                    {
                        customOrder.OrderDetails.Remove(odt);
                        return true;
                    }
                }
            }

            return true;
        }

        /// <summary>
        /// Kiem tra so luong con co the mua duoc
        /// </summary>
        /// <param name="dealID"></param>
        /// <returns></returns>
        private int CheckQuantityAvalidableByDeal(int dealID)
        {
            OrderDetailBL odtb = new OrderDetailBL();
            List<OrderDetail> list = new List<OrderDetail>();
            list = odtb.GetAllOrderDetailByDeal(dealID);
            int kq = 0;
            foreach (OrderDetail item in list)
            {
                kq += ConvertManage.ConvertToInt(item.Quantity_i,0);
            }
            DealBL dealBL = new DealBL();
            Deal deal = new Deal();

            deal = dealBL.GetByID(dealID);
            kq = ConvertManage.ConvertToInt(deal.CurrentQuantity_i,0) - kq;

            return kq;
        }
        /// <summary>
        /// cap nhat chay hang
        /// </summary>
        /// <param name="deal"></param>
        private void UpdateSoldOut(Deal deal)
        {
            DealBL dealBL = new DealBL();
            deal.Status_i = ConvertManage.ConvertToInt(DealStatus.SoldOut, 0);
            dealBL.Update(deal);

        }
    }
}
